package cm.com.blackboard.DataModule4B2.customizeConfigUtils;

import java.beans.IntrospectionException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.JDOMException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.blackboard.DataModule4B2.util.BatchImportModel;
import cn.com.blackboard.DataModule4B2.util.ExtentionModel;

public class BatchImportUtil {
private static final Logger logger = LoggerFactory.getLogger(BatchImportUtil.class);
	
	static String profilepath=CommonConfigLoader.class.getClassLoader().getResource("/batchImportMap.xml").getPath();
	//static String profilepath = System.getProperty("user.dir")+"/target/classes/datasourcekeyMap.xml";
       
	private static List<BatchImportModel> batchImportModelList = new ArrayList<BatchImportModel> ();
	static { 
		try {
			batchImportModelList = BaseXMLUtil.parseXML(profilepath, BatchImportModel.class);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	
	 public static boolean setAndPersistExtentionList(List<BatchImportModel> biList){
		batchImportModelList = biList;
		try {
			BaseXMLUtil.renderXML(profilepath, ExtentionModel.class, batchImportModelList);
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			return false;
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			return false;
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			return false;
		} catch (IntrospectionException e) {
			e.printStackTrace();
			return false;
		}
	}
 
	public static List<BatchImportModel> getBatchImportModelList() {
		return batchImportModelList;
	}

	public static void setBatchImportModelList(List<BatchImportModel> batchImportModelList) {
		BatchImportUtil.batchImportModelList = batchImportModelList;
	}



	private static Map<String, String> model2table = new HashMap<String,String>();
	static {
		model2table.put("CourseInfo", "BB_COURSE_INFO");
		model2table.put("DepartmentInfo", "BB_DEPARTMENT_INFO");
		model2table.put("StudentInfo", "BB_STUDENT_INFO");
		model2table.put("TeacherInfo", "BB_STAFF_INFO");
		model2table.put("StudentCourseInfo", "BB_STUDENT_COURSE");
		model2table.put("TeacherCourseInfo", "BB_TEACHER_COURSE");
	}
	
	public static String getBatchFilterCondition(String modelClsName){
		BatchImportModel biModel = null;
		String result = "";
		for(BatchImportModel tmpBiModel:batchImportModelList){
			if(tmpBiModel.getModelClsName().equalsIgnoreCase(modelClsName))
				biModel = tmpBiModel;
		}
		if(biModel != null){
			String tablename = model2table.get(biModel.getModelClsName());
			result = tablename+"."+biModel.getRefferRules();
		}
		else
			result = "1=1";
		return result;
	}
}
